from django.shortcuts import render
from django.contrib.auth.models import User
from rest_framework.authtoken.views import APIView, AuthTokenSerializer
from rest_framework.authtoken.models import Token
from rest_framework.response import Response

# Create your views here.


# 用户注册
# class Register(APIView):
#     def post(self, request, *args, **kwargs):
#         username = request.data.get("userName")
#         password = request.data.get("password")
#         if User.objects.filter(username=username).exists():
#             response = {
#                 "status": False,
#                 "data": "用户名已被注册"
#             }
#         else:
#             user = User.objects.create_user(username=username, password=password)
#             token, created = Token.objects.get_or_create(user=user)
#             response = {
#                 'status': True,
#                 'token': token.key,
#                 'user_id': user.pk,
#                 'user_name': user.username,
#                 'password': user.password,
#             }
#         return Response(response)
#
#
# # 用户登录
# class Login(APIView):
#     def post(self, request, *args, **kwargs):
#         print(request.data)
#         serializer = AuthTokenSerializer(data=request.data, context={'request': request})
#         serializer.is_valid(raise_exception=True)
#         user = serializer.validated_data['user']
#         token, created = Token.objects.get_or_create(user=user)
#         return Response({
#             'code': 0,
#             'status': True,
#             'token': token.key,
#             'user_id': user.pk,
#             'user_name': user.username
#         })

from rest_framework_simplejwt.views import TokenObtainPairView
from .serializer import MyToekenObtainPairSerializer


class MyToekenObtainPairView(TokenObtainPairView):
    serializers_class = MyToekenObtainPairSerializer
